package org.ithang.application.bcAddr.service;

import java.util.List;
import java.util.Map;
import org.ithang.application.bcAddr.mapper.BcAddrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;
import org.apache.shiro.codec.Hex;
import org.ithang.tools.model.Pager;
import org.ithang.tools.model.GlobalException;
import org.ithang.tools.database.ModelDao;
import org.ithang.tools.eth.ETHInfo;
import org.ithang.tools.eth.ETHUtil;
import org.ithang.tools.lang.StrUtils;
import org.ithang.application.bcAddr.bean.BcAddr;

/**
 * 充币地址管理 服务
 * @author zyt
 *
 */
@Service
public class BcAddrService extends ModelDao<BcAddr>{

    @Autowired
    private BcAddrMapper bcAddrMapper;

    private Logger logger = Logger.getLogger(BcAddrService.class);
    
    public void add(BcAddr bcAddr){
    	ETHInfo ei=ETHUtil.createWallet();
    	if(null!=ei){
    		bcAddr.setAddr(ei.getData().get("address"));
        	//bcAddr.setPrivate_key(Hex.encodeToString(ei.getData().get("privateKey").getBytes()));
    		bcAddr.setPrivate_key(StrUtils.ehx(ei.getData().get("privateKey")));
        	bcAddr.setPublic_key(ei.getData().get("publicKey"));
    	}else{
    		logger.error("创建充币地址失败,原因：创建链地址失败!");
    		throw new GlobalException(100,"创建充币地址失败,原因：创建链地址失败!");
    	}
    	
        bcAddrMapper.add(bcAddr);
    }
    
    public BcAddr get(Integer id){
        return bcAddrMapper.get(id);
    }
    
    public BcAddr getByAddr(String addr){
        return bcAddrMapper.getByAddr(addr);
    }
    
    public BcAddr getByCustomer(Integer customer_id){
        return bcAddrMapper.getByCustomer(customer_id);
    }
    
    public int delete(Integer id){
        return bcAddrMapper.delete(id);
    }
    
    public int batchDelete(String[] ids){
    	return bcAddrMapper.batchDelete(ids);
    }
    
    public List<BcAddr> list(Integer... ids){
    	return bcAddrMapper.list(ids);
    }
    
    public List<BcAddr> page(Pager<BcAddr> page){
    	return bcAddrMapper.page(page);
    }
    
    public List<BcAddr> query(Map<String,Object> conditions){
    	return bcAddrMapper.query(conditions);
    }
    
    public int update(BcAddr bcAddr){
    	return bcAddrMapper.update(bcAddr);
    }
    
    
}